package org.gocom.components.coframe.auth.login;

import java.util.Iterator;

import org.gocom.components.coframe.tools.IConstants;

import com.alibaba.fastjson.JSONObject;
import com.eos.data.datacontext.UserObject;
import com.eos.foundation.data.DataObjectUtil;
import com.eos.system.annotation.Bizlet;

import commonj.sdo.DataObject;

@Bizlet
public class JSONAndDataObjectUtil {

	@Bizlet
	public static DataObject parseJSON2DataObjectOrg(JSONObject json,String entity){
		DataObject data = DataObjectUtil.createDataObject(entity);
		Iterator<String> it = json.keySet().iterator();
		while(it.hasNext()){
			String key = it.next();
			Object v;
			v = json.get(key);
			data.set(key, v);
		}
		return data;
	}
	
	@Bizlet
	public static DataObject parseJSON2DataObjectOrg(String jsonStr,String entity){
		return parseJSON2DataObjectOrg(JSONObject.parseObject(jsonStr), entity);
	}
	
	@Bizlet
	public static UserObject parseJSON2UserObject(JSONObject json){
		Iterator<String> it = json.keySet().iterator();
		UserObject userObject = new UserObject();
		while(it.hasNext()){
			String key = it.next();
			//Object v;
			if("empid".equals(key)){
				userObject.setUserId(json.getString(key));
				userObject.setUserName(json.getString(key));
			} else if("empname".equals(key)){
				userObject.setUserRealName(json.getString(key));
			} else if("orgid".equals(key)){
				userObject.setUserOrgId(json.getString(key));
			} else if("orgseq".equals(key)){
				userObject.put(IConstants.ORG_PATH, json.getString(key));
			} else{
				continue;
			}
		}
		
		return userObject;
	}
		
	@Bizlet
	public static UserObject parseJSON2UserObject(String jsonStr){
		return parseJSON2UserObject(JSONObject.parseObject(jsonStr));
	}
	
	
}
